﻿using System;
using System.Collections.Generic;
using System.Data;
using PPI.Entities.Distribution;
using PPI.General;

namespace PPI.WebConsumer.Distribution
{
    public class WPole
    {
        public static Int32 Save(EDistributionPole pole)
        {
            ConfigReader reader = ConfigReader.GetInstance();
            PPI.Svc.Service1 svc = new PPI.Svc.Service1();

            int sectionNumber;
            if (!Int32.TryParse(pole.Section.Name, out sectionNumber))
                sectionNumber = pole.Section.Id;

            return svc.DistributionPole_Save(pole.ServerID,
                                            pole.Map.Id, 
                                            sectionNumber, 
                                            pole.Specie.Id, 
                                            pole.Class.Id, 
                                            pole.Length.Id, 
                                            pole.Treatment.Id, 
                                            pole.Latitude, 
                                            pole.Longitude, 
                                            pole.InspBy, 
                                            pole.Number, 
                                            pole.AcquisitionDate, 
                                            pole.Year, 
                                            pole.LVisit, 
                                            (Int32)pole.DistributionType, 
                                            pole.D804,
                                            pole.Vendor.Id,
                                            pole.Project.Id,
                                            pole.VendorContact.Id,
                                            pole.GuardsQuantity,
                                            pole.PoleTag,
                                            pole.GroundWireRepair,
                                            pole.Billable,
                                            pole.Circuit,
                                            pole.Kv.Id,
                                            pole.ConductorType.Id,
                                            pole.ACSR.Id,
                                            reader.GetConnectionString());
        }

        public static List<ENote> Save(Int32 poleId, List<ENote> notes)
        {
            ConfigReader reader = ConfigReader.GetInstance();
            PPI.Svc.Service1 svc = new PPI.Svc.Service1();

            List<PPI.Svc.ENote> wNotes = new List<PPI.Svc.ENote>();
            foreach (ENote note in notes)
            {
                PPI.Svc.ENote en = new PPI.Svc.ENote();

                en.Id = note.Id;
                en.Key = note.Key;
                en.Name = note.Name;

                wNotes.Add(en);
            }

            PPI.Svc.ENote[] wENotesResult = svc.DistributionNote_Save(poleId, wNotes.ToArray(), reader.GetConnectionString());
            notes = new List<ENote>();
            foreach (PPI.Svc.ENote noteResult in wENotesResult)
            {
                ENote nResp = new ENote();

                nResp.ServerId = noteResult.ServerId;
                nResp.Id = noteResult.Id;
                nResp.Key = noteResult.Key;
                nResp.Name = noteResult.Name;

                notes.Add(nResp);
            }

            return notes;
        }

        public static List<EMeter> Save(Int32 poleId, List<EMeter> meters)
        {
            ConfigReader reader = ConfigReader.GetInstance();
            PPI.Svc.Service1 svc = new PPI.Svc.Service1();

            List<PPI.Svc.EMeter> wMeters = new List<PPI.Svc.EMeter>();
            foreach (EMeter meter in meters)
            {
                PPI.Svc.EMeter met = new PPI.Svc.EMeter();

                met.Id = meter.Id;
                met.Comments = meter.Comments;
                met.Latitude = meter.Latitude;
                met.Longitude = meter.Longitude;
                met.Number = meter.Number;

                wMeters.Add(met);
            }

            PPI.Svc.EMeter[] wEMetersResult = svc.DistributionMeter_Save(poleId, wMeters.ToArray(), reader.GetConnectionString());
            meters = new List<EMeter>();
            foreach (PPI.Svc.EMeter meterResult in wEMetersResult)
            {
                EMeter mResp = new EMeter();

                mResp.ServerId = meterResult.ServerId;
                mResp.Id = meterResult.Id;

                meters.Add(mResp);
            }

            return meters;
        }

        public static List<ETransformer> Save(Int32 poleId, List<ETransformer> transformers)
        {
            ConfigReader reader = ConfigReader.GetInstance();
            PPI.Svc.Service1 svc = new PPI.Svc.Service1();

            List<PPI.Svc.ETransformer> wTransformers = new List<PPI.Svc.ETransformer>();
            foreach (ETransformer transformer in transformers)
            {
                PPI.Svc.ETransformer trans = new PPI.Svc.ETransformer();

                trans.Id = transformer.Id;
                trans.Comments = transformer.Comments;
                trans.Latitude = transformer.Latitude;
                trans.Longitude = transformer.Longitude;
                trans.SerialNumber = transformer.SerialNumber;
                trans.CreatedDate = transformer.CreatedDate;

                trans.Arrestor = transformer.Arrestor;
                trans.DualRated = transformer.DualRated;
                trans.Manufacturer = transformer.Manufacturer;
                trans.PPM = transformer.PPM;
                trans.Pre1978 = transformer.Pre1978;
                trans.CutOut = transformer.CutOut;
                trans.Sticker = transformer.Sticker;
                trans.TransformerNumber = transformer.TransformerNumber;
                trans.Primary = transformer.Primary;
                trans.Secondary = transformer.Secondary;
                trans.UtilityNumber = transformer.UtilityNumber;
                trans.TestNumber = transformer.TestNumber;
                trans.Impeddance = transformer.Impeddance;
                trans.Taps = transformer.Taps;
                trans.Polarity = transformer.Polarity;
                trans.ManufDate = transformer.ManufDate;
                trans.Sticker = transformer.Sticker;

                trans.Class = new PPI.Svc.EDistributionLookUp();
                trans.PCBType = new PPI.Svc.EDistributionLookUp();
                trans.Phase = new PPI.Svc.EDistributionLookUp();
                trans.TransformerType = new PPI.Svc.EDistributionLookUp();
                trans.Type = new PPI.Svc.EDistributionLookUp();
                trans.ArrestorKv = new PPI.Svc.EDistributionLookUp();
                trans.CutOutKv = new PPI.Svc.EDistributionLookUp();
                trans.Can = new PPI.Svc.EDistributionLookUp();
                trans.DeterminedBy = new PPI.Svc.EDistributionLookUp();
                
                trans.Class.Id = transformer.Class.Id;
                trans.PCBType.Id = transformer.PCBType.Id;
                trans.Phase.Id = transformer.Phase.Id;
                trans.TransformerType.Id = transformer.TransformerType.Id;
                trans.Type.Id = transformer.Type.Id;
                trans.ArrestorKv.Id = transformer.ArrestorKv.Id;
                trans.CutOutKv.Id = transformer.CutOutKv.Id;
                trans.Can.Id = transformer.Can.Id;
                trans.DeterminedBy.Id = transformer.DeterminedBy.Id;

                wTransformers.Add(trans);
            }

            PPI.Svc.ETransformer[] wETransformersResult = svc.DistributionTransformer_Save(poleId, wTransformers.ToArray(), reader.GetConnectionString());
            transformers = new List<ETransformer>();
            
            foreach (PPI.Svc.ETransformer transformerResult in wETransformersResult)
            {
                ETransformer mResp = new ETransformer();

                mResp.ServerId = transformerResult.ServerId;
                mResp.Id = transformerResult.Id;

                transformers.Add(mResp);
            }

            return transformers;
        }


        public static List<ESectionalizer> Save(Int32 poleId, List<ESectionalizer> sectionalizers)
        {
            ConfigReader reader = ConfigReader.GetInstance();
            var svc = new PPI.Svc.Service1();

            var wSectionalizers = new List<PPI.Svc.ESectionalizer>();
            foreach (ESectionalizer sectionalizer in sectionalizers)
            {
                var sect = new PPI.Svc.ESectionalizer();

                sect.Id = sectionalizer.Id;
                sect.Comments = sectionalizer.Comments;
                sect.CreatedDate = sectionalizer.CreatedDate;
                sect.CurveA = sectionalizer.CurveA;
                sect.CurveB = sectionalizer.CurveB;
                sect.CurveC = sectionalizer.CurveC;
                sect.CurveD = sectionalizer.CurveD;
                sect.Latitude = sectionalizer.Latitude;
                sect.Longitude = sectionalizer.Longitude;
                sect.Manufacturer = sectionalizer.Manufacturer;
                sect.ManufDate = sectionalizer.ManufDate;
                sect.MaxkV = sectionalizer.MaxkV;
                sect.PoleNumber = sectionalizer.PoleNumber;
                sect.PPM = sectionalizer.PPM;
                sect.Pre1978 = sectionalizer.Pre1978;
                sect.Sticker = sectionalizer.Sticker;
                sect.SerialNumber = sectionalizer.SerialNumber;
                sect.TestNumber = sectionalizer.TestNumber;
                sect.Type = sectionalizer.Type;
                
                sect.DeterminedBy = new PPI.Svc.EDistributionLookUp();
                sect.Interrupter = new PPI.Svc.EDistributionLookUp();
                sect.PCBType = new PPI.Svc.EDistributionLookUp();
                sect.Phase = new PPI.Svc.EDistributionLookUp();

                sect.DeterminedBy.Id = sectionalizer.DeterminedBy.Id;
                sect.Interrupter.Id = sectionalizer.Interrupter.Id;
                sect.PCBType.Id = sectionalizer.PCBType.Id;
                sect.Phase.Id = sectionalizer.Phase.Id;

                wSectionalizers.Add(sect);
            }

            PPI.Svc.ESectionalizer[] wESectionalizersResult = svc.DistributionSectionalizer_Save(poleId, wSectionalizers.ToArray(), reader.GetConnectionString());
            sectionalizers = new List<ESectionalizer>();

            foreach (PPI.Svc.ESectionalizer sectionalizerResult in wESectionalizersResult)
            {
                var mResp = new ESectionalizer();

                mResp.ServerId = sectionalizerResult.ServerId;
                mResp.Id = sectionalizerResult.Id;

                sectionalizers.Add(mResp);
            }

            return sectionalizers;
        }

        public static List<ERecloser> Save(Int32 poleId, List<ERecloser> reclosers)
        {
            ConfigReader reader = ConfigReader.GetInstance();
            var svc = new PPI.Svc.Service1();

            var wReclosers = new List<PPI.Svc.ERecloser>();
            foreach (ERecloser recloser in reclosers)
            {
                var recl = new PPI.Svc.ERecloser();

                recl.Id = recloser.Id;
                recl.Comments = recloser.Comments;
                recl.CreatedDate = recloser.CreatedDate;
                recl.CurveA = recloser.CurveA;
                recl.CurveB = recloser.CurveB;
                recl.CurveC = recloser.CurveC;
                recl.CurveD = recloser.CurveD;
                recl.Latitude = recloser.Latitude;
                recl.Longitude = recloser.Longitude;
                recl.Manufacturer = recloser.Manufacturer;
                recl.ManufDate = recloser.ManufDate;
                recl.MaxkV = recloser.MaxkV;
                recl.PoleNumber = recloser.PoleNumber;
                recl.PPM = recloser.PPM;
                recl.Pre1978 = recloser.Pre1978;
                recl.Sticker = recloser.Sticker;
                recl.SerialNumber = recloser.SerialNumber;
                recl.TestNumber = recloser.TestNumber;
                recl.Type = recloser.Type;

                recl.DeterminedBy = new PPI.Svc.EDistributionLookUp();
                recl.Interrupter = new PPI.Svc.EDistributionLookUp();
                recl.PCBType = new PPI.Svc.EDistributionLookUp();
                recl.Phase = new PPI.Svc.EDistributionLookUp();

                recl.DeterminedBy.Id = recloser.DeterminedBy.Id;
                recl.Interrupter.Id = recloser.Interrupter.Id;
                recl.PCBType.Id = recloser.PCBType.Id;
                recl.Phase.Id = recloser.Phase.Id;

                wReclosers.Add(recl);
            }

            PPI.Svc.ERecloser[] wERecloserResult = svc.DistributionRecloser_Save(poleId, wReclosers.ToArray(), reader.GetConnectionString());
            reclosers = new List<ERecloser>();

            foreach (PPI.Svc.ERecloser recloserResult in wERecloserResult)
            {
                var mResp = new ERecloser();

                mResp.ServerId = recloserResult.ServerId;
                mResp.Id = recloserResult.Id;

                reclosers.Add(mResp);
            }

            return reclosers;
        }

        public static List<EDistributionLookUp> SaveSectionalizerNotes(int sectionalizerId, List<EDistributionLookUp> notes)
        {
            ConfigReader reader = ConfigReader.GetInstance();
            var svc = new PPI.Svc.Service1();
            var wNotes = new List<PPI.Svc.EDistributionLookUp>();
            foreach (Entities.Distribution.EDistributionLookUp note in notes)
            {
                PPI.Svc.EDistributionLookUp n = new PPI.Svc.EDistributionLookUp();

                n.Id = note.Id;
                n.Text = note.Text;

                wNotes.Add(n);
            }

            svc.DistributionNotesSectionalizers_Save(sectionalizerId, wNotes.ToArray(), reader.GetConnectionString());

            return notes;
        }

        public static List<EDistributionLookUp> SaveRecloserNotes(int recloserId, List<EDistributionLookUp> notes)
        {
            ConfigReader reader = ConfigReader.GetInstance();
            var svc = new PPI.Svc.Service1();
            var wNotes = new List<PPI.Svc.EDistributionLookUp>();
            foreach (Entities.Distribution.EDistributionLookUp note in notes)
            {
                PPI.Svc.EDistributionLookUp n = new PPI.Svc.EDistributionLookUp();

                n.Id = note.Id;
                n.Text = note.Text;

                wNotes.Add(n);
            }

            svc.DistributionNotesReclosers_Save(recloserId, wNotes.ToArray(), reader.GetConnectionString());

            return notes;
        }

        public static List<EDistributionLookUp> SaveTransformerNotes(int transformerId, List<Entities.Distribution.EDistributionLookUp> notes)
        {
            ConfigReader reader = ConfigReader.GetInstance();
            PPI.Svc.Service1 svc = new PPI.Svc.Service1();
            List<PPI.Svc.EDistributionLookUp> wNotes = new List<PPI.Svc.EDistributionLookUp>();
            foreach (Entities.Distribution.EDistributionLookUp note in notes)
            {
                PPI.Svc.EDistributionLookUp n = new PPI.Svc.EDistributionLookUp();

                n.Id = note.Id;
                n.Text = note.Text;
                
                wNotes.Add(n);
            }

            svc.DistributionNotesTransformers_Save(transformerId, wNotes.ToArray(), reader.GetConnectionString());

            return notes;
        }

        public static List<ENote> SaveMeterNotes(Int32 meterId, List<ENote> notes)
        {
            ConfigReader reader = ConfigReader.GetInstance();
            PPI.Svc.Service1 svc = new PPI.Svc.Service1();
            List<PPI.Svc.ENote> wNotes = new List<PPI.Svc.ENote>();
            foreach (ENote note in notes)
            {
                PPI.Svc.ENote n = new PPI.Svc.ENote();

                n.Id = note.Id;
                n.Key = note.Key;
                n.Name = note.Name;
                n.ServerId = note.ServerId;

                wNotes.Add(n);
            }

            PPI.Svc.ENote[] wENotesResult = svc.DistributionNotesMeter_Save(meterId, wNotes.ToArray(), reader.GetConnectionString());
            notes = new List<ENote>();
            foreach (PPI.Svc.ENote noteResult in wENotesResult)
            {
                ENote nResp = new ENote();

                nResp.ServerId = noteResult.ServerId;
                nResp.Id = noteResult.Id;
                nResp.Key = noteResult.Key;
                nResp.Name = noteResult.Name;

                notes.Add(nResp);
            }

            return notes;
        }

        public static Int32 Save(string fileName, Int32 poleId)
        {
            ConfigReader reader = ConfigReader.GetInstance();
            PPI.Svc.Service1 svc = new PPI.Svc.Service1();

            return svc.DistributionPoleImage_Save(poleId, fileName, reader.GetConnectionString());
        }

        public static void SaveMeterImage(string fileName, Int32 meterId)
        {
            ConfigReader reader = ConfigReader.GetInstance();
            PPI.Svc.Service1 svc = new PPI.Svc.Service1();

            svc.DistributionMeterImage_Save(meterId, fileName, reader.GetConnectionString());
        }

        public static Int32 SaveTransformerImage(string fileName, Int32 transformerId)
        {
            ConfigReader reader = ConfigReader.GetInstance();
            PPI.Svc.Service1 svc = new PPI.Svc.Service1();

            return svc.DistributionTransformerImage_Save(transformerId, fileName, reader.GetConnectionString());
        }

        public static Int32 SaveSectionalizerImage(string fileName, Int32 sectionalizerId)
        {
            ConfigReader reader = ConfigReader.GetInstance();
            PPI.Svc.Service1 svc = new PPI.Svc.Service1();

            return svc.DistributionSectionalizerImage_Save(sectionalizerId, fileName, reader.GetConnectionString());
        }

        public static Int32 SaveRecloserImage(string fileName, Int32 recloserId)
        {
            ConfigReader reader = ConfigReader.GetInstance();
            PPI.Svc.Service1 svc = new PPI.Svc.Service1();

            return svc.DistributionRecloserImage_Save(recloserId, fileName, reader.GetConnectionString());
        }

        public static Int32 Save(EDistributionInspectionResult inspectionResult, Int32 poleId)
        {
            ConfigReader reader = ConfigReader.GetInstance();
            PPI.Svc.Service1 svc = new PPI.Svc.Service1();

            return svc.DistributionInspectionResult_Save(poleId, inspectionResult.ServerId, inspectionResult.InspectionType.Id, inspectionResult.Resultype.Id, inspectionResult.IsElectrical, inspectionResult.Comments, reader.GetConnectionString());
        }

        public static void Update(EDistributionInspectionResult inspectionResult, Int32 poleId)
        {
            ConfigReader reader = ConfigReader.GetInstance();
            PPI.Svc.Service1 svc = new PPI.Svc.Service1();
            
            svc.DistributionInspectionResult_Update(poleId, inspectionResult.ServerId, inspectionResult.InspectionType.Id, inspectionResult.Resultype.Id, inspectionResult.IsElectrical, inspectionResult.Comments, reader.GetConnectionString());
        }

        public static DataSet GetMetersWithoutImage()
        {
            ConfigReader reader = ConfigReader.GetInstance();
            PPI.Svc.Service1 svc = new PPI.Svc.Service1();
            return svc.DistributionMeter_GetWithoutImages(reader.GetConnectionString());
        }

        public static DataSet SelectAllLookUps()
        {
            ConfigReader reader = ConfigReader.GetInstance();
            PPI.Svc.Service1 svc = new PPI.Svc.Service1();
            return svc.Distribution_GetLookUps(reader.GetConnectionString());
        }
        

        public static Int32 Update(EDistributionPole pole)
        {
            ConfigReader reader = ConfigReader.GetInstance();
            PPI.Svc.Service1 svc = new PPI.Svc.Service1();
            
            return svc.DistributionPole_Update(pole.Id,
                                            pole.Map.Id,
                                            pole.Section.Id,
                                            pole.Specie.Id,
                                            pole.Class.Id,
                                            pole.Length.Id,
                                            pole.Treatment.Id,
                                            pole.Latitude,
                                            pole.Longitude,
                                            pole.InspBy,
                                            pole.Number,
                                            pole.AcquisitionDate,
                                            pole.Year,
                                            pole.LVisit,
                                            (Int32)pole.DistributionType,
                                            pole.D804,
                                            pole.Vendor.Id,
                                            pole.Project.Id,
                                            pole.Inspector.Id,
                                            pole.GuardsQuantity,
                                            pole.PoleTag,
                                            pole.GroundWireRepair,
                                            pole.Billable,
                                            pole.Circuit, 
                                            pole.Kv.Id,
                                            reader.GetConnectionString());
        }

        public static List<ENote> Update(Int32 poleId, List<ENote> notes)
        {
            ConfigReader reader = ConfigReader.GetInstance();
            PPI.Svc.Service1 svc = new PPI.Svc.Service1();
            
            List<PPI.Svc.ENote> wNotes = new List<PPI.Svc.ENote>();
            foreach (ENote note in notes)
            {
                PPI.Svc.ENote en = new PPI.Svc.ENote();

                en.Id = note.Id;
                en.Key = note.Key;
                en.Name = note.Name;

                wNotes.Add(en);
            }

            PPI.Svc.ENote[] wENotesResult = svc.DistributionNote_Update(poleId, wNotes.ToArray(), reader.GetConnectionString());
            notes = new List<ENote>();
            foreach (PPI.Svc.ENote noteResult in wENotesResult)
            {
                ENote nResp = new ENote();

                nResp.ServerId = noteResult.ServerId;
                nResp.Id = noteResult.Id;
                nResp.Key = noteResult.Key;
                nResp.Name = noteResult.Name;

                notes.Add(nResp);
            }

            return notes;
        }

        public static EMeter Update(Int32 poleId, EMeter meter)
        {
            ConfigReader reader = ConfigReader.GetInstance();
            PPI.Svc.Service1 svc = new PPI.Svc.Service1();
            PPI.Svc.EMeter met = new PPI.Svc.EMeter();

            met.Id = meter.Id;
            met.Comments = meter.Comments;
            met.Latitude = meter.Latitude;
            met.Longitude = meter.Longitude;
            met.Number = meter.Number;

            List<PPI.Svc.ENote> wNotes = new List<PPI.Svc.ENote>();
            foreach (ENote note in meter.Notes)
            {
                PPI.Svc.ENote n = new PPI.Svc.ENote();

                n.Id = note.Id;
                n.Key = note.Key;
                n.Name = note.Name;
                n.ServerId = note.ServerId;

                wNotes.Add(n);
            }

            met.Notes = wNotes.ToArray();

            PPI.Svc.EMeter mResp = svc.DistributionMeter_Update(poleId, met, reader.GetConnectionString());
            EMeter meterUpdated = new EMeter();
            meterUpdated.ServerId = mResp.ServerId;
            meterUpdated.Id = mResp.Id;

            return meterUpdated;
        }

        public static void DeletePoleImages(int[] poleImagesIds)
        {
            ConfigReader reader = ConfigReader.GetInstance();
            PPI.Svc.Service1 svc = new PPI.Svc.Service1();
            
            svc.DistributionPole_DeleteImages(poleImagesIds, reader.GetConnectionString());
        }

        public static void DeleteMeter(Int32 meterId)
        {
            ConfigReader reader = ConfigReader.GetInstance();
            PPI.Svc.Service1 svc = new PPI.Svc.Service1();

            svc.DistributionMeter_Delete(meterId, reader.GetConnectionString());
        }

        public static void DeletePole(Int32 poleId)
        {
            ConfigReader reader = ConfigReader.GetInstance();
            PPI.Svc.Service1 svc = new PPI.Svc.Service1();

            svc.DistributionPole_Delete(poleId, reader.GetConnectionString());
        }
        
    } 
}
